package com.itheima.service.impl;

import com.itheima.dao.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.WebUserLoginService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.IdWorker;
import com.itheima.utils.IpUtils;
import com.itheima.utils.RequestUtils;
import com.itheima.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.Map;
@Service
public class WebUserLoginServiceImpl implements WebUserLoginService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public Map userLogin(UserVo userVO) {
        if (!StringUtils.isEmpty(userVO.getUserName()) || !StringUtils.isEmpty(userVO.getPassWord())) {
            Example example = new Example(User.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("status", 1);
            criteria.andEqualTo("userName", userVO.getUserName());
            User user = userMapper.selectOneByExample(example);//数据库查询出的对象

            if (user == null) {
                throw new RuntimeException("账户或密码错误,请重新登录");
            } else {
                HashMap map = new HashMap();
                //BCrypt密码校验,获取一个Boolean值
                Boolean login = BCrypt.checkpw(userVO.getPassWord(), user.getPassWord());
                //设置登录次数
                user.setLoginCount(user.getLoginCount() + 1);
                user.setLastLoginTime(DateUtils.getTimestamp());
                user.setLastLoginIp(IpUtils.getIpAddr(RequestUtils.getHttpServletRequest()));
                map.put("login", login);
                map.put("user", user);
                return map;
                //判断令牌中的内容是否合格,这里用的BCrypt.checkpw()算法,这是一种不可逆算法,用来对密码进行加密和校对
            }
        }
        return null;
    }

    @Override
    public Integer userRegister(UserVo userVO) {
        User user = new User();
        //用户名
        user.setUserName(userVO.getUserName());
        User userByUserName = userMapper.selectOne(user);
        if (userByUserName != null) {
            return 0;
        }

        //雪花 uid
        Long uid = new IdWorker().nextId();
        user.setUid(uid.toString());
        user.setUserName(userVO.getUserName());

        //密码加密
        //获取盐
        String gensalt = BCrypt.gensalt();
        String hashpw = BCrypt.hashpw(userVO.getPassWord(), gensalt);
        user.setPassWord(hashpw);

        //性别
        user.setGender(Integer.parseInt(userVO.getGender()));
        //个人头像
        user.setAvatar(userVO.getAvatar());
        //邮箱

        user.setEmail(userVO.getEmail());
        user.setBirthday(userVO.getBirthday());
        user.setMobile(userVO.getMobile());
        // user.setValidCode(userVO.getUserName());
        //自我简介
        user.setSummary(userVO.getSummary());
        //user.setLoginCount(userVO.getUserName());
        //user.setLastLoginTime(userVO.getUserName());
        //user.setLastLoginIp(userVO.getUserName());
        //状态
        user.setStatus(userVO.getStatus());
        //创建时间和更新时间
        user.setCreateTime(DateUtils.getTimestamp());
        user.setUpdateTime(DateUtils.getTimestamp());
        //昵称
        user.setNickName(userVO.getNickName());
        //资料来源
        user.setSource(userVO.getSource());
        //平台uuid
        user.setUuid(userVO.getUuid());
        //qq号
        user.setQqNumber(userVO.getQqNumber());
        //微信号
        user.setWeChat(userVO.getWeChat());
        //职业
        user.setOccupation(userVO.getOccupation());
        //评论状态
        user.setCommentStatus(userVO.getCommentStatus());
        //ip地址
        user.setIpSource(IpUtils.getIpAddr(RequestUtils.getHttpServletRequest()));
        //获取当前浏览器
        user.setBrowser(IpUtils.getOsAndBrowserInfo(RequestUtils.getHttpServletRequest()).get("BROWSER"));
        //获取当前系统
        user.setOs(IpUtils.getOsAndBrowserInfo(RequestUtils.getHttpServletRequest()).get("OS"));
        //是否开启邮件通知
        user.setStartEmailNotification(userVO.getStartEmailNotification());
        //用户标签
        user.setUserTag(userVO.getUserTag());

        int num = userMapper.insertSelective(user);

        return num;
    }
}
